/**
 * 
 */
package com.googlecode.lohr.parser.combinators.matcher;


import java.util.ArrayList;

import com.googlecode.lohr.parser.combinators.MatchResults;
import com.googlecode.lohr.parser.combinators.node.AbstractNode;

public class LongestMatcher extends ChoiceMatcher
{
	public LongestMatcher(Matcher... matchers)
	{
		super(matchers);
	}

	@Override
	public MatchResults match(MatchContext ctx, int start) 
	{
		MatchResults results= super.match(ctx, start);
		if (!results.success())
			return results;
		AbstractNode longestResult= null;
		for (AbstractNode match : results.matches)
			if (longestResult == null || longestResult.getEnd() < match.getEnd())
				longestResult= match;

		ArrayList<AbstractNode> l= new ArrayList<AbstractNode>();
		l.add(longestResult);
		return new MatchResults(l);
	}
}